package com.firefly.netty.demo;

import com.firefly.dao.util.RedisUtils;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.json.JsonObjectDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @description:
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/9/14
 **/
@Component
@Slf4j
public class NettyServer {

    @Autowired
    private RedisUtils redisUtils;

    //2.声明本类
    private static NettyServer nettyServer;

    //3.声明构造方案
    public NettyServer() {
    }

    @PostConstruct
    public void init() {
        nettyServer = this;
        nettyServer.redisUtils = this.redisUtils;
    }
    private final int port = 9601; //监听端口

    //编写run方法，处理客户端的请求
    public void run() throws Exception {
        // 启动前清理所有相关的redis数据
        nettyServer.redisUtils.del("channel-client");
        nettyServer.redisUtils.del("code-client");
        nettyServer.redisUtils.del("channel-code");
        nettyServer.redisUtils.del("code-online");
        //创建两个线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup(); //8个NioEventLoop
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 2048)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //获取到pipeline
                            ChannelPipeline pipeline = ch.pipeline();
                            // json解码器
                            pipeline.addLast(new JsonObjectDecoder(512));
                            // StringDecoder 解码器,这应该是最简单的解码器
                            pipeline.addLast(new StringDecoder());
                            pipeline.addLast(new StringEncoder());
                            //向pipeline加入自定义的解码器
//                            pipeline.addLast(new DataFrameDecoder());
//                            pipeline.addLast(new DataFrameEncoder());
                            //加入自己的业务处理handler
                            pipeline.addLast(new NettyServerHandler());
                        }
                    });
            log.debug("netty start port:" + port + "......");
            ChannelFuture channelFuture = b.bind(port).sync();
            //监听关闭
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }
}
